/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CapaPresentacion;

import CapaLogica.Artista;
import CapaLogica.Gestor;
import CapaLogica.MultiArtista;
import java.util.ArrayList;
import java.util.Date;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Elayner
 */
public class PantallaObraEditar extends javax.swing.JFrame {

    private static Gestor gestor=new Gestor();
    private static TreeMap datosArtistas;
    private static TreeMap datosGaleria;    
    private String infoArtista;
    private String infoGaleria;
    private String codigo;
    private String nomObra;
    private String dimensiones;
    private String fechaRealizada;
    private String duracionDesarrollo;
    private String tecnica;
    private String estado;
    private boolean indFama;
    private String desHistoria;
    private int codArtista;
    private String nomGaleria;
    private int pindex_Artista;
    private int pindex_Galeria;
    private String costo;
    private Date fechaActual=new Date();
    private String fechaAdqui;
    
    public PantallaObraEditar(String pcodObra) throws Exception {
        initComponents();
        loadScreenObra(pcodObra);       
    
    }
    
    public PantallaObraEditar() {
        initComponents();   
    }    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cod_obra = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        nom_obra = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        dimen_obra = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tec_obra = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        fec_real_obra = new com.toedter.calendar.JDateChooser();
        estado_obra = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        dur_obra = new javax.swing.JTextField();
        ind_famo_obra = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        his_desc_obra = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        btn_actualizar = new javax.swing.JButton();
        btn_cancelar = new javax.swing.JButton();
        nom_galeria = new javax.swing.JLabel();
        nom_gal = new javax.swing.JTextField();
        arti_info = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("P");

        jLabel1.setText("Codigo:");

        cod_obra.setEditable(false);

        jLabel2.setText("Nombre:");

        jLabel3.setText("Estado:");

        jLabel4.setText("Dimensiones:");

        jLabel5.setText("Tecnica Utilzada");

        jLabel6.setText("Fecha realizada:");

        estado_obra.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Buena", "Regular", "Mala" }));

        jLabel7.setText("Duracion desarrollo (meses):");

        ind_famo_obra.setText("Es famosa");

        his_desc_obra.setColumns(20);
        his_desc_obra.setRows(5);
        jScrollPane1.setViewportView(his_desc_obra);

        jLabel8.setText("Historia:");

        jLabel9.setText("Artista:");

        btn_actualizar.setText("Actualizar");
        btn_actualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_actualizarActionPerformed(evt);
            }
        });

        btn_cancelar.setText("Cancelar");
        btn_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelarActionPerformed(evt);
            }
        });

        nom_galeria.setText("Galeria");

        nom_gal.setEditable(false);

        arti_info.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(28, 28, 28)
                        .addComponent(nom_obra, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(dimen_obra, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cod_obra, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(dur_obra))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel6)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(fec_real_obra, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel9)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(arti_info, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nom_galeria)
                                .addGap(5, 5, 5)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(estado_obra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(48, 48, 48))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nom_gal)
                                .addContainerGap())))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(107, 107, 107)
                                .addComponent(ind_famo_obra))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(31, 31, 31)
                                .addComponent(tec_obra, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(41, 41, 41))))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(295, 295, 295)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(198, 198, 198)
                        .addComponent(btn_actualizar)
                        .addGap(18, 18, 18)
                        .addComponent(btn_cancelar)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cod_obra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(tec_obra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nom_obra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(estado_obra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ind_famo_obra))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(dimen_obra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nom_galeria)
                    .addComponent(nom_gal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(fec_real_obra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(dur_obra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(arti_info, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_actualizar)
                    .addComponent(btn_cancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btn_cancelarActionPerformed

    private void btn_actualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_actualizarActionPerformed
        if(validarPantalla()){
            try {
                codigo=cod_obra.getText();
                desHistoria=his_desc_obra.getText();
                nomObra=nom_obra.getText();
                dimensiones=dimen_obra.getText();
                fechaRealizada=helperUI.getDateFormatConv(fec_real_obra.getDate());
                duracionDesarrollo=dur_obra.getText();
                tecnica=tec_obra.getText();
                estado=(String)estado_obra.getSelectedItem();
                indFama=ind_famo_obra.isSelected();
                desHistoria=his_desc_obra.getText();
         
                if(gestor.actualizarObra(codigo, nomObra, dimensiones, fechaRealizada, duracionDesarrollo,
                        tecnica, estado, indFama, desHistoria, codArtista))
                    this.setVisible(false);
                
            } catch (Exception ex) {
                Logger.getLogger(PantallaObraEditar.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
             JOptionPane.showMessageDialog(this,(String) "Debe de llenar los campos requeridos","Error",JOptionPane.ERROR_MESSAGE);	
        }
    }//GEN-LAST:event_btn_actualizarActionPerformed
    public void loadScreenObra(String pcodObra) throws Exception{
        TreeMap infoObra;
        infoObra=gestor.buscarInfoGeneralObra(pcodObra);
        TreeMap datosArtistas=gestor.buscarInfoGeneralArtista((int)infoObra.get("idArtista"));
        TreeMap datosAdquisicion=gestor.buscarInfoAdquisicion(pcodObra);
        
        if(infoObra!=null && datosArtistas !=null && datosAdquisicion!=null){           
           cod_obra.setText((String)infoObra.get("codigo"));
           nom_obra.setText((String)infoObra.get("nombre"));
           dimen_obra.setText((String)infoObra.get("dimensiones"));
           fec_real_obra.setDate(helperUI.getDateFormatDate((String)infoObra.get("fechaRealizada")));
           dur_obra.setText((String)infoObra.get("duracionDesa"));
           tec_obra.setText((String)infoObra.get("tecnica"));
           estado_obra.setSelectedItem((String)infoObra.get("estado"));
           ind_famo_obra.setSelected((boolean)infoObra.get("indFama"));
           arti_info.setText((String)datosArtistas.get("nombre"));
           his_desc_obra.setText((String)infoObra.get("desHistoria"));
           nom_gal.setText((String)datosAdquisicion.get("nomGaleria"));
        }
    }
    
    private boolean validarPantalla(){
        boolean result = true;
        if(cod_obra.getText().length()<=0)
            result=false;
        if(nom_obra.getText().length()<=0)
            result=false;
        if(dimen_obra.getText().length()<=0)
            result=false;
        if(fec_real_obra.getDate()== null)
            result=false;
        if(dur_obra.getText().length()<=0)
            result=false;
        if(tec_obra.getText().length()<=0)
            result=false;
        if(estado_obra.getSelectedItem()==null)
            result=false;
        if(ind_famo_obra.getText().length()<=0)
            result=false;
        if(his_desc_obra.getText().length()<=0)
            result=false;  
        return result;
    }    
    
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaObraEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaObraEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaObraEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaObraEditar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PantallaObraEditar().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField arti_info;
    private javax.swing.JButton btn_actualizar;
    private javax.swing.JButton btn_cancelar;
    private javax.swing.JTextField cod_obra;
    private javax.swing.JTextField dimen_obra;
    private javax.swing.JTextField dur_obra;
    private javax.swing.JComboBox estado_obra;
    private com.toedter.calendar.JDateChooser fec_real_obra;
    private javax.swing.JTextArea his_desc_obra;
    private javax.swing.JCheckBox ind_famo_obra;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nom_gal;
    private javax.swing.JLabel nom_galeria;
    private javax.swing.JTextField nom_obra;
    private javax.swing.JTextField tec_obra;
    // End of variables declaration//GEN-END:variables
}
